const router = require('koa-router')()

//用户管理列表
router.get('/getwuliu',async(ctx,next)=>{
    let sql = 'select * from feedback'
    let res = await ctx.db.query(sql)
 
    ctx.body = {'code':200,'msg':res}
  
  })
  
  //搜索
  router.post('/search',async(ctx,next)=>{
    let key = ctx.request.body.key
    console.log(key);
    let sql = "select * from feedback where openid like concat('%',?,'%') || contact like concat('%',?,'%')"
    let res = await ctx.db.query(sql,[key,key])
    console.log(res);
    ctx.body = {'code':200,'msg':res}
  })

  //更新数据
  router.post('/updatawuliu',async(ctx,next)=>{
    let row = ctx.request.body
    row.edit = false
    console.log(row)
    let sql = 'update feedback set openid=?,solution=?,contact=? where id=?'
    await ctx.db.query(sql,[row.openid,row.solution,row.contact,row.id])

    ctx.body={'code':200,"msg":"编辑成功"}
  
  })
  
  //添加用户数据
  router.post('/addwuliu', async(ctx,next)=>{
    let row = ctx.request.body
    console.log(row);
    let sql = 'insert into feedback(openid,solution,contact) values(?,?,?)'
    await ctx.db.query(sql,[row.openid,row.solution,row.contact])
    
    ctx.body={'code':200,'msg':"添加成功"}
  
  })
  
  
  //删除某个用户数据
  router.post('/deletewuliu',async(ctx,next)=>{
    let id = ctx.request.body.id
    let sql='delete from feedback where id=?'
    await ctx.db.query(sql,[id])

    ctx.body={"code":200,"msg":'删除成功'}
  })

  module.exports = router